package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.ReservationMemberAddDto;
import com.zzyl.nursing.vo.CountByTimeVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationMemberService;
import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 * @date 2025-08-28
 */
@RestController
@RequestMapping("/member/reservation")
public class ReservationMemberController extends BaseController
{
    @Autowired
    private IReservationMemberService reservationService;

    /**
     * 查询预约登记列表
     */
    @GetMapping("/page")
    public AjaxResult page(Reservation reservation) {
        /*startPage();*/
        List<Reservation> list = reservationService.selectReservationList(reservation);
        // 将查询结果封装为 TableDataInfo
        // 返回分页数据
        TableDataInfo tableDataInfo = getDataTable(list);
        return success(tableDataInfo);
    }


    /**
     * 取消预约
     */
    @PutMapping("/{id}/cancel")
    public AjaxResult cancel(@PathVariable Long id) {

        return reservationService.cancelReservation(id);
    }


    /**
     * 查询取消预约数量
     * @return
     */
    @GetMapping("/cancelled-count")
    public AjaxResult cancelledCount() {
        return reservationService.getCancelledCountByUserId();
    }


    /**
     * 查询每个时间段剩余预约次数
     * @param time 时间戳（long）
     * @return AjaxResult
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public R<List<CountByTimeVo>> countReservationsForEachTimeWithinTimeRange(Long time) {
        List<CountByTimeVo> list = reservationService.countReservationsForTime(time);
        return R.ok(list);
    }

    /**
     * 新增预约信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody ReservationMemberAddDto dto)
    {
        reservationService.insertReservation(dto);
        return success();
    }







    /**
     * 获取预约信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(reservationService.selectReservationById(id));
    }


    /**
     * 修改预约信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:edit')")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Reservation reservation)
    {
        return toAjax(reservationService.updateReservation(reservation));
    }


}
